<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-10-18
 * Time: 14:54
 */
namespace app\index\controller;

use think\Db;

class Staffsave extends Base
{
    public function index()
    {//添加员工页面展示
        $this->baseindex();
        //原表单数据

        //所有角色
        $role = Db::table('oa_role')->where('state=1 or state=2')->select();

        $month = Db::table('oa_month')->select();

        $this->assign('month', $month);

        $this->assign('role', $role);

        return $this->fetch();
    }

//员工拥有一个或以上角色时
    public function staffsave_form()
    {//添加员工数据处理
        $this->baseindex();
        //return 110;
        if (isset($_POST['submit']) && $_POST['submit'] == 'submit') {
            $result_aa = input('post.data');
            $result = json_decode($result_aa, true);
            //return $result;
            //$key = array_keys($result);
            $ch_name = $result['ch_name'];
            $pass = $result['password'];
            $en_name = $result['en_name'];
            //邮箱 中英文用户名  部门 职位 不能为空
            if (!empty($result['email']) && !empty($result['ch_name']) && !empty($result['branch_id']) && !empty($result['position']) && !empty($result['en_name'])) {
                //验证邮箱
                $yan_email = $this->getEmaila($result['email']);
                //return 120;
                if ($yan_email) {
                    //验证邮箱是否唯一
                    $verity_email=$this->verify_email($result['email']);
                    if (!$verity_email){
                        return 3;
                    }
                    //验证手机号唯一
                    $verity_phone=$this->verify_phone($result['tel']);
                    if (!$verity_phone){
                        return 4;
                    }
                    //验证中英文用户名是否唯一
                    $ch_name_res = Db::table('oa_user')->where(['ch_name' => $ch_name])->find();
                    $en_name_res = Db::table('oa_user')->where(['en_name' => $en_name])->find();
                    if (empty($ch_name_res) && empty($en_name_res)) {
                        //return 1122;
                        if ($pass == '') {
                            $result['password'] = md5(123456);
                        } else {
                            $result['password'] = md5($pass);
                        }
                        //return $result;
                        $user_id = Db::table('oa_user')->insertGetId($result);
                        //return $user_id;
                        $role = Db::table('oa_role')->select();
                        //return $role;
                        foreach ($role as $k => $v) {
                            $datas['user_id'] = $user_id;
                            $datas['role_id'] = $v['id'];
                            Db::table('oa_user_role')->insert($datas);
                        }
                        //return 324;
                        $key = array_keys($result);
                        if (count($key) > 4) {
                            //return 333;
                            for ($i = 0; $i < count($key); $i++) {
                                //强制转化成int
                                $key_s = intval($key[$i]);
                                if ($key_s != 0) {
                                    if ($result[$key[$i]] == "on") {
                                        $date['user_id'] = $user_id;
                                        $date['role_id'] = $key[$i];
                                        //$date['state'] =1;
                                        Db::table('oa_user_role')->where($date)->update(['state' => 1]);
                                        //return $num;
                                    }
                                }
                            }
                        }
                        return 1;
                    } else {
                        return 2;
                        //header('location: ' . $_SERVER['HTTP_REFERER']);
                    }
                } else {
                    //验证邮箱是否重复（暂未启用）
                    return 3;
                    //header('location: ' . $_SERVER['HTTP_REFERER']);
                }
            } else {
                return 0;
                //header('location: ' . $_SERVER['HTTP_REFERER']);
            }
        } else {
            // 非法入侵
            return 10;
            //$this->redirect('Login/index');
        }
    }
}